package Main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import com.mysql.jdbc.Statement;
public class ShoppingCar{
	SelectCarGoods g = new SelectCarGoods();
	String url = "jdbc:mysql://localhost:3306/goods";
    String userName = "root";
    String password = "kamisama";
    String driverName = "com.mysql.jdbc.Driver";
    String strSql = "insert into cars(name,price,num,description) values(?,?,?,?)";
    Scanner sc = new Scanner(System.in);
    	Connection con1 = null;
    	PreparedStatement pstmt1 = null;
    	ResultSet rs1 = null;
    	String driverName2 = "com.mysql.jdbc.Driver";
    public void AddGoods(){                        //将购买商品的信息导入购物车
    	try {
			Class.forName(driverName2);
	        con1 = (Connection) DriverManager.getConnection(url, userName, password); // 根据参数的插入数据
	        pstmt1 = con1.prepareStatement(strSql);
	        pstmt1.setString(1,g.rs.getString("name"));                     //获取商品名
	        pstmt1.setDouble(2, g.rs.getDouble("price"));                 //获取商品价格
	        pstmt1.setInt(3, g.rs.getInt("num"));                                //获取商品数量
	        pstmt1.setString(4, g.rs.getString("description"));           //获取商品描述
	        pstmt1.executeUpdate();
	       /* while(rs1.next()){
	        	int id = rs1.getInt("id");
	            String name = rs1.getString("name");
	            Double price = rs1.getDouble("price");
	            int num = rs1.getInt("num");
	            String description = rs1.getString("description");
	            System.out.print("id=" + id + "  name="+name+ " price="+price+"  num=" + num+" description="+description+"\n");
	        }*/
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (SQLException e){
			e.printStackTrace();
		}finally {
            if (rs1 != null)
                try {
                    rs1.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            if (pstmt1 != null){
                try {
                    pstmt1.close();// 关闭语句*/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (con1 != null) {
                try {
                    con1.close();// 关闭连接
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
	}
}
